home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 06 - 1990 / 06.11 Nov 90 / Kelly Source / HunkMain.p < prev   
Encoding:
Text File  |  1990-09-03  |  15.2 KB  |  671 lines  |  [TEXT/PJMM]

  1. program HunkManagerDemo;
  2.  
  3. {This demo program requires}
  4. {the Hunk Manager, available from:}
  5. {The Gettys Group, Inc.}
  6. {401 East Illinois Street, Suite 600}
  7. {Chicago, Illinois 60611}
  8. {312-836-4222}
  9.  
  10. {Demo program by Dave Kelly}
  11. {©1990 MacTutor}
  12.  
  13.     uses
  14.         Hunks;
  15.  
  16.     const
  17.         appleID = 128;
  18.         fileID = 129;
  19.         editID = 130;
  20.         HunkID = 131;
  21.  
  22.         appleMenu = 1;
  23.         fileMenu = 2;
  24.         editMenu = 3;
  25.         HunkMenu = 4;
  26.         menuCount = 4;
  27.         windowID = 128;
  28.         GetNewHunkitem = 1;
  29.         GetHunkitem = 2;
  30.         CloseHunkitem = 3;
  31.         ListHunkitem = 1;
  32.         SortHunkitem = 2;
  33.         Quititem = 5;
  34.         undoitem = 1;
  35.         cutitem = 3;
  36.         copyitem = 4;
  37.         pasteitem = 5;
  38.         clearitem = 6;
  39.         isShared = false;
  40.  
  41.     var
  42.         myMenus: array[1..menuCount] of MenuHandle;
  43.         ignore: integer;
  44.         dragRect: Rect;
  45.         theChar: CHAR;
  46.         extended: BOOLEAN;
  47.         doneFlag: BOOLEAN;
  48.         myEvent: EventRecord;
  49.         wRecord: WindowRecord;
  50.         myWindow: Windowptr;
  51.         whichWindow: Windowptr;
  52.         windowsize: longint;
  53.         height, width: integer;
  54.         sizeRect, size: Rect;
  55.         Result: OSErr;
  56.         hasWNE: boolean;
  57.         cursorRgn: RgnHandle;    {for WaitNextEvent}
  58.         Hunkfilename: string[63];
  59.         HunkvRefNum, numHandles: integer;
  60.         fileRef: HFilePtr;    {Hunk file pointer}
  61.         checkTime, timeOut: longint;
  62.         MyHunkString, MySortedstring: array[1..10] of str255;
  63.         theType: HunkType;
  64.         theID: longint;
  65.  
  66.     procedure swap (var a: str255; var b: str255);
  67.         var
  68.             temp: str255;
  69.     begin
  70.         temp := a;
  71.         a := b;
  72.         b := temp;
  73.     end;
  74.  
  75.     procedure sortarray;
  76.         var
  77.             i, j: integer;
  78.     begin
  79.         for i := 1 to 10 do
  80.             MySortedstring[i] := MyHunkString[i];
  81.         for i := 1 to 9 do
  82.             begin
  83.                 for j := i + 1 to 10 do
  84.                     if MySortedstring[i] > MySortedString[j] then
  85.                         swap(MySortedstring[i], MySortedstring[j]);
  86.             end;
  87.     end;
  88.  
  89.     procedure GetPredefinedHunks;
  90.         var
  91.             i: integer;
  92.             myhandle: handle;
  93.             theString: str255;
  94.     begin
  95.         for i := 1 to 10 do
  96.             begin
  97.                 MyHunkString[i] := '';
  98.                 theID := i + 1000;
  99.                 MyHandle := (GetHunk(fileRef, 'STR#', theID));
  100.                 Result := HunkError(FileRef);
  101.                 MyHunkString[i] := stringhandle(myhandle)^^;
  102.             end;
  103.     end;
  104.  
  105.     procedure GetSortedHunks;
  106.         var
  107.             i: integer;
  108.             myhandle: handle;
  109.     begin
  110.         for i := 1 to 10 do
  111.             begin
  112.                 MySortedString[i] := '';
  113.                 theID := i + 1000;
  114.                 MyHandle := GetHunk(fileRef, 'SORT', theID);
  115.                 MySortedString[i] := stringhandle(myhandle)^^;
  116.             end;
  117.     end;
  118.  
  119.     procedure addsortedHunks;
  120.         var
  121.             i: integer;
  122.             MyHandle: handle;
  123.     begin
  124.         for i := 1 to 10 do
  125.             begin
  126.                 MyHandle := Handle(NewString(MySortedString[i]));
  127.                 theID := i + 1000;
  128.                 AddHunk(fileRef, MyHandle, 'SORT', theID);
  129.                 WriteHunk(fileRef, MyHandle);
  130.                 disableitem(myMenus[HunkMenu], 2);
  131.             end;
  132.     end;
  133.  
  134.     procedure showHunk;
  135.         var
  136.             HunkCount, Hunksize, NumberofHunks, theindex: longint;
  137.             vertspacing, i: integer;
  138.  
  139.     begin
  140.         eraserect(sizerect);    {clear the screen}
  141.         hunkCount := CountHunkTypes(fileRef);
  142.         vertspacing := 50;
  143.         moveto(10, vertspacing);
  144.         writeDraw('There are ', hunkCount, ' hunk type(s) in the file: ', hunkfilename);
  145.         vertspacing := vertspacing + 12;
  146.         moveto(10, vertspacing);
  147.         writeDraw('The Hunk Type(s) are: ');
  148.         for theindex := 1 to hunkcount do
  149.             begin
  150.                 vertspacing := vertspacing + 12;
  151.                 moveto(10, vertspacing);
  152.                 GetIndHunkType(fileRef, theType, theIndex);
  153.                 Hunksize := GetHunkTypeSize(fileRef, theType);
  154.                 NumberofHunks := countHunks(fileRef, theType);
  155.                 writeDraw(theType, ',  ', hunksize, ' bytes in ', NumberofHunks, ' Hunks.');
  156.                 if theType = 'STR#' then
  157.                     begin
  158.                         vertspacing := vertspacing + 12;
  159.                         moveto(10, vertspacing);
  160.                         GetPredefinedHunks;
  161.                         for i := 1 to 10 do
  162.                             writedraw(MyHunkString[i], ' ');
  163.                     end;
  164.                 if theType = 'SORT' then
  165.                     begin
  166.                         vertspacing := vertspacing + 12;
  167.                         moveto(10, vertspacing);
  168.                         GetSortedHunks;
  169.                         for i := 1 to 10 do
  170.                             writedraw(MySortedString[i], ' ');
  171.                     end;
  172.             end;
  173.         if MySortedString[1] = '' then
  174.             enableitem(myMenus[HunkMenu], 2);
  175.     end;
  176.  
  177.     procedure AddPredefinedHunks;
  178.         var
  179.             i: integer;
  180.             myhandle: handle;
  181.     begin
  182.         MyHunkString[1] := 'Memory';
  183.         MyHunkString[2] := 'Scanner';
  184.         MyHunkString[3] := 'Modem';
  185.         MyHunkString[4] := 'Floppy Disk';
  186.         MyHunkString[5] := 'Monitor';
  187.         MyHunkString[6] := 'Keyboard';
  188.         MyHunkString[7] := 'Mouse';
  189.         MyHunkString[8] := 'Printer';
  190.         MyHunkString[9] := 'Macintosh';
  191.         MyHunkString[10] := 'Hard Disk';
  192.         for i := 1 to 10 do
  193.             begin
  194.                 MyHandle := Handle(NewString(MyHunkString[i]));
  195.                 theID := i + 1000;
  196.                 AddHunk(fileRef, MyHandle, 'STR#', theID);
  197.                 WriteHunk(fileRef, MyHandle);
  198.             end;
  199.  
  200.     end;
  201.  
  202.     procedure GetNewFilename;
  203. {Get a new filename and then create a new Hunk file}
  204.  
  205.         var
  206.             prompt, origName: str255;
  207.             where: point;
  208.             reply: SFReply;
  209.             i: integer;
  210.  
  211.         const
  212.             HunkCreator = 'BHun';
  213.             HunkfileType = 'HTxt';
  214.  
  215.     begin
  216.         where.h := 50;
  217.         where.v := 50;
  218.         prompt := 'Enter New Data Filename:';
  219.         origName := '';
  220.         SFPutFile(where, prompt, origName, nil, reply);
  221.         HunkFileName := reply.fName;
  222.         HunkvRefNum := reply.vRefNum;
  223.         moveto(10, 50);
  224.         if Reply.good then
  225.             begin
  226.                 Result := CreateHunkFile(HunkfileName, HunkvRefNum, Hunkcreator, HunkfileType);
  227.                 if Result <> 0 then
  228.                     begin  {error occurred}
  229.                         if result = -48 then
  230.                             begin  {error was -48}
  231.                                 Result := FSDelete(HunkFileName, HunkvRefNum);
  232.                                 if Result = 0 then  {file was deleted}
  233.                                     begin
  234.                                         Result := CreateHunkFile(HunkfileName, HunkvRefNum, Hunkcreator, HunkfileType);
  235.                                         if result <> 0 then
  236.                                             begin
  237.                                                 moveto(10, 50);
  238.                                                 writedraw('Error= ', result, ' occurred.');
  239.                                                 exit(GetNewFileName);
  240.                                             end;
  241.                                         fileRef := OpenHunkFile(HunkfileName, HunkvRefNum, numHandles, checkTime, timeOut, isShared);
  242.                                         AddPredefinedHunks;
  243.                                     end
  244.                                 else  {there is an error deleting the file}
  245.                                     begin
  246.                                         moveto(10, 50);
  247.                                         writedraw('Error= ', result, ' occurred.');
  248.                                         exit(GetNewFileName);
  249.                                     end;
  250.                             end;  {error was -48}
  251.                     end {error occurred}
  252.                 else {no error occurred}
  253.                     begin
  254.                         fileRef := OpenHunkFile(HunkfileName, HunkvRefNum, numHandles, checkTime, timeOut, isShared);
  255.                         AddPredefinedHunks;
  256.                     end;
  257.             end;
  258.         for i := 1 to 10 do
  259.             MySortedString[i] := '';
  260.     end;
  261.  
  262.     procedure GetFilename;
  263. {Get an existing Hunk file and open it}
  264.  
  265.         var
  266.             prompt: str255;
  267.             where: point;
  268.             reply: SFReply;
  269.             typeList: SFTypeList;
  270.             numTypes: integer;
  271.  
  272.  
  273.         const
  274.             HunkCreator = 'BHun';
  275.             HunkfileType = 'HTxt';
  276.  
  277.     begin
  278.         where.h := 50;
  279.         where.v := 50;
  280.         numTypes := 1;
  281.         TypeList[0] := 'HTxt';
  282.         prompt := 'Select Data Filename:';
  283.         SFGetFile(where, prompt, nil, numTypes, typeList, nil, reply);
  284.         HunkFileName := reply.fName;
  285.         HunkvRefNum := reply.vRefNum;
  286.         fileRef := OpenHunkFile(HunkfileName, HunkvRefNum, numHandles, checkTime, timeOut, isShared);
  287.         result := hunkerror(fileref);
  288.         if result <> 0 then
  289.             begin
  290.                 moveto(10, 50);
  291.                 writedraw('Error= ', result, ' occurred.');
  292.                 exit(GetFileName);
  293.             end;
  294.         GetPredefinedHunks;
  295.         if counthunktypes(fileref) = 2 then
  296.             GetSortedHunks;
  297.     end;
  298.  
  299.     procedure Closefile;
  300.     begin
  301.         if fileRef <> nil then
  302.             closeHunkFile(fileRef);
  303.     end;
  304.  
  305.     function TrapAvailable (tNumber: INTEGER; tType: TrapType): BOOLEAN;
  306.  
  307. {To check if WaitNextEvent Trap is available; the following 2 functions are found in TN#158 }
  308.  
  309.         const
  310.             UnimplementedTrapNumber = $A89F;  {number of "unimplemented trap"}
  311.  
  312.     begin {TrapAvailable}
  313.  
  314.     {Check and see if the trap exists.}
  315.     {On 64K ROM machines, tType will be ignored.}
  316.  
  317.         TrapAvailable := (NGetTrapAddress(tNumber, tType) <> GetTrapAddress(UnimplementedTrapNumber));
  318.  
  319.     end;  {TrapAvailable}
  320.  
  321.     function WNEIsImplemented: BOOLEAN;
  322.  
  323.         const
  324.             WNETrapNumber = $A860; {trap number of WaitNextEvent}
  325.  
  326.         var
  327.             theWorld: SysEnvRec; {to check if machine has new traps}
  328.             discardError: OSErr; {to ignore OSErr return from SysEnvirons}
  329.  
  330.     begin {WNEIsImplemented}
  331.  
  332. {  Since WaitNextEvent and HFSDispatch both have the same trap }
  333. { number ( $60 ) , we can only call TrapAvailable }
  334. { for WaitNextEvent if we are on a machine that supports separate }
  335. { OS and Toolbox trap tables . We call SysEnvirons and check }
  336. { if machineType < 0. }
  337.  
  338.         discardError := SysEnvirons(1, theWorld);
  339.  
  340. {  Even if we got an error from SysEnvirons, the SysEnvirons glue }
  341. { has set up machineType . }
  342.  
  343.         if theWorld.machineType < 0 then
  344.             WNEIsImplemented := FALSE
  345.           {this ROM doesn't have separate trap tables or WaitNextEvent}
  346.         else
  347.             WNEIsImplemented := TrapAvailable(WNETrapNumber, ToolTrap);
  348.           {check for WaitNextEvent}
  349.  
  350.     end;  {WNEIsImplemented}
  351.  
  352.     procedure initialize_managers;
  353.  
  354.         var
  355.             versRequested: integer;
  356.             theWorld: SysEnvRec;
  357.  
  358.     begin
  359.         InitGraf(@thePort);
  360.         InitFonts;
  361.         InitWindows;
  362.         InitMenus;
  363.         TEInit;
  364.         InitDialogs(nil);
  365.         InitCursor;
  366.         FlushEvents(everyEvent, 0);
  367.  
  368.         Result := SysEnvirons(versRequested, theWorld); { Refer to TN#129 for use of the SysEnvirons Function }
  369.         if theWorld.machineType < 0 then
  370.             begin
  371.                 hasWNE := false;
  372.             end
  373.         else
  374.             begin
  375.                 hasWNE := WNEIsImplemented;
  376.             end;
  377.     end;
  378.  
  379.     procedure SetUpMenus;
  380.         var
  381.             i: integer;
  382.     begin
  383.         myMenus[appleMenu] := GetMenu(AppleID);
  384.         AddResMenu(myMenus[appleMenu], 'DRVR');
  385.         myMenus[fileMenu] := GetMenu(fileID);
  386.         myMenus[editMenu] := GetMenu(editID);
  387.         myMenus[HunkMenu] := GetMenu(HunkID);
  388.         for i := 1 to menuCount do
  389.             InsertMenu(myMenus[i], 0);
  390.         DrawMenuBar;
  391.         disableitem(myMenus[HunkMenu], 1);
  392.         disableitem(myMenus[fileMenu], 3);
  393.         disableitem(myMenus[HunkMenu], 2);
  394.     end;
  395.  
  396.     procedure DoCommand (mResult: LONGINT);
  397.         var
  398.             theItem: integer;
  399.             theMenu: integer;
  400.             editmenu: menuhandle;
  401.             name: Str255;
  402.             temp: integer;
  403.         const
  404.             AboutID = 128;
  405.             AboutItem = 1;
  406.  
  407.     begin
  408.         theItem := LoWord(mResult);
  409.         theMenu := HiWord(mResult);
  410.         case theMenu of
  411.             appleID: 
  412.                 begin
  413.                     case theItem of
  414.                         Aboutitem: 
  415.                             ignore := Alert(AboutID, nil);
  416.                         otherwise
  417.                             begin
  418.                                 if FrontWindow = nil then
  419.                                     begin
  420.                                         EnableItem(EditMenu, UndoItem);
  421.                                         EnableItem(EditMenu, CutItem);
  422.                                         EnableItem(EditMenu, CopyItem);
  423.                                         EnableItem(EditMenu, Pasteitem);
  424.                                         EnableItem(EditMenu, ClearItem);
  425.                                     end;
  426.                                 GetItem(myMenus[appleMenu], theItem, name);
  427.                                 temp := OpenDeskAcc(name);
  428.                                 SetPort(myWindow);
  429.                             end;
  430.                     end; { case }
  431.                 end;
  432.             fileID: 
  433.                 case theItem of
  434.                     GetNewHunkitem: 
  435.                         begin
  436.                             GetNewFilename;
  437.                             if (fileRef <> nil) and (Result = 0) then
  438.                                 begin
  439.                                     disableitem(myMenus[fileMenu], 1);
  440.                                     disableitem(myMenus[fileMenu], 2);
  441.                                     enableitem(myMenus[fileMenu], 3);
  442.                                     enableitem(myMenus[HunkMenu], 1);
  443.                                     eraserect(sizerect);    {clear the screen}
  444.                                 end;
  445.                         end;
  446.                     GetHunkitem: 
  447.                         begin
  448.                             GetFilename;
  449.                             if (fileRef <> nil) and (Result = 0) then
  450.                                 begin
  451.                                     disableitem(myMenus[fileMenu], 1);
  452.                                     disableitem(myMenus[fileMenu], 2);
  453.                                     enableitem(myMenus[fileMenu], 3);
  454.                                     enableitem(myMenus[HunkMenu], 1);
  455.                                     eraserect(sizerect);    {clear the screen}
  456.                                 end;
  457.                         end;
  458.                     closeHunkitem: 
  459.                         begin
  460.                             closefile;
  461.                             enableitem(myMenus[fileMenu], 1);
  462.                             enableitem(myMenus[fileMenu], 2);
  463.                             disableitem(myMenus[fileMenu], 3);
  464.                             disableitem(myMenus[HunkMenu], 1);
  465.                             eraserect(sizerect);    {clear the screen}
  466.                         end;
  467.                     Quititem: 
  468.                         begin
  469.                             closefile;
  470.                             doneFlag := TRUE;
  471.                         end;
  472.                 end;
  473.             editID: 
  474.                 begin
  475.                     if not SystemEdit(theItem - 1) then
  476.                         case theItem of
  477.                             cutitem: 
  478.                                 ;
  479.                             copyitem: 
  480.                                 ;
  481.                             pasteitem: 
  482.                                 ;
  483.                             clearitem: 
  484.                                 ;
  485.                             otherwise
  486.                                 ;
  487.                         end; { Case }
  488.                 end; { editID }
  489.             HunkID: 
  490.                 begin
  491.                     case theItem of
  492.                         ListHunkitem: 
  493.                             showhunk;
  494.                         SortHunkitem: 
  495.                             begin
  496.                                 sortarray;
  497.                                 addsortedHunks;
  498.                                 showhunk;
  499.                             end;
  500.                         otherwise
  501.                             ;
  502.                     end;
  503.                 end;{ HunkID }
  504.             otherwise
  505.                 ;
  506.         end; { menuCase }
  507.         HiliteMenu(0);
  508.     end;
  509.  
  510.     procedure DoEvent (Event: EventRecord);
  511.     begin
  512.         case myEvent.what of
  513.             mouseDown: 
  514.                 case FindWindow(myEvent.where, whichWindow) of
  515.                     inDesk: 
  516.                         ;
  517.                     inMenuBar: 
  518.                         DoCommand(MenuSelect(myEvent.where));
  519.                     inSysWindow: 
  520.                         SystemClick(myEvent, whichWindow);
  521.                     inContent: 
  522.                         begin
  523.                             if whichWindow <> FrontWindow then
  524.                                 SelectWindow(whichWindow)
  525.                             else
  526.                                 begin
  527.                                     GlobalToLocal(myEvent.where);
  528.                                     extended := BitAnd(myEvent.modifiers, shiftKey) <> 0;
  529.                                 end;
  530.                         end;
  531.                     inDrag: 
  532.                         DragWindow(whichWindow, myEvent.where, dragRect);
  533.                     inGrow: 
  534.                         begin
  535.                             windowsize := GrowWindow(whichWindow, myEvent.where, sizeRect);
  536.                             height := Hiword(windowsize);
  537.                             width := LoWord(windowsize);
  538.                             SizeWindow(whichWindow, width, height, TRUE);
  539.                             setrect(size, 0, 0, width - 15, height);
  540.                             invalRect(size);
  541.                             setrect(size, 0, 0, width, height - 15);
  542.                             invalRect(size);
  543.                             EraseRect(thePort^.portRect);
  544.                             DrawGrowIcon(whichWindow);
  545.                         end;
  546.                     inGoAway: 
  547.                         if TrackGoAway(whichWindow, myEvent.where) then
  548.                             begin
  549.                                 CloseWindow(whichWindow);
  550.                                 closefile;
  551.                                 doneFlag := TRUE;
  552.                             end;
  553.                 end;
  554.             mouseUp: 
  555.                 begin
  556.                 end;
  557.             keydown, autokey: 
  558.                 begin
  559.                     theChar := CHR(BitAnd(myEvent.message, charCodeMask));
  560.                     if BitAnd(myEvent.modifiers, cmdKey) <> 0 then
  561.                         DoCommand(MenuKey(theChar))
  562.                     else
  563.                         ;
  564.                 end;
  565.             keyUp: 
  566.                 begin
  567.                 end;
  568.             updateEvt: 
  569.                 begin
  570.                     BeginUpdate(WindowPtr(myEvent.message));
  571. {    EraseRect(thePort^.portRect);}
  572.                     EndUpdate(WindowPtr(myEvent.message));
  573.                     DrawGrowIcon(WindowPtr(myEvent.message));
  574.                 end;
  575.             diskEvt: 
  576.                 begin
  577.                 end;
  578.             activateEvt: 
  579.                 begin
  580.                     if BitAnd(myEvent.modifiers, activeFlag) <> 0 then
  581.                         begin
  582.                             ;
  583.                             DisableItem(myMenus[editMenu], undoitem);
  584.                         end
  585.                     else
  586.                         begin
  587.                             ;
  588.                             EnableItem(myMenus[editMenu], undoitem);
  589.                         end;
  590.                 end;
  591.             networkEvt: 
  592.                 begin
  593.                 end;
  594.             driverEvt: 
  595.                 begin
  596.                 end;
  597.             app1Evt: 
  598.                 begin
  599.                 end;
  600.             app2Evt: 
  601.                 begin
  602.                 end;
  603.             app3Evt: 
  604.                 begin
  605.                 end;
  606.             otherwise
  607.                 begin
  608.                 end;
  609.         end;
  610.     end;
  611.  
  612.     function GetSleep: longint;
  613.         var
  614.             Sleep: longint;
  615.     begin
  616.         Sleep := 10;  {a sleep algorithm can be inserted here}
  617.         GetSleep := Sleep;
  618.     end;
  619.  
  620.     function GetEvent: boolean;
  621.         var
  622.             theEvent: boolean;
  623.     begin
  624.         if hasWNE then
  625.             begin
  626.                 theEvent := WaitNextEvent(everyEvent, MyEvent, GetSleep, cursorRgn);
  627.             end
  628.         else
  629.             begin
  630.                 SystemTask;
  631.                 theEvent := GetNextEvent(everyEvent, MyEvent);
  632.             end;
  633.         GetEvent := theEvent;
  634.     end; {GetEvent}
  635.     procedure DoIdle;
  636.     begin
  637. {Don't do anything}
  638.     end;
  639.  
  640.     procedure EventLoop;  {from IM vol 6}
  641.         var
  642.             gotEvent: boolean;
  643.     begin
  644.         cursorRgn := NewRgn; {pass an empty region the first time thru}
  645.         repeat
  646.             gotEvent := GetEvent;
  647. {    AdjustCursor(event.where, cursorRgn);   for future use}
  648.             if gotEvent then
  649.                 DoEvent(Myevent)
  650.             else
  651.                 DoIdle;
  652.         until doneFlag {loop forever}
  653.     end;
  654.  
  655. begin
  656.     Initialize_Managers;
  657.     SetUpMenus;
  658.     with screenBits.bounds do
  659.         SetRect(dragRect, 4, 24, right - 4, bottom - 4);
  660.     sizeRect := dragRect;
  661.     doneFlag := FALSE;
  662.     fileRef := nil;        {Hunk file pointer}
  663.     checktime := 1;     {used for shared Hunk files}
  664.     timeout := 1;         {used for shared Hunk files}
  665.     numhandles := 2;    {minimum number of Hunk Handles is 44}
  666.     myWindow := GetNewwindow(windowID, @wRecord, POINTER(-1));
  667.     SetPort(myWindow);
  668.     textfont(Geneva);
  669.     textsize(10);
  670.     EventLoop;
  671. end.